// 移除顶部导入的jest
import { mount } from '@vue/test-utils'
import Login from '../components/Login.vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../stores/user'

// 使用全局jest对象进行mock
jest.mock('element-plus', () => ({
  ElMessage: {
    success: jest.fn(),
    error: jest.fn(),
  },
}));

jest.mock('../stores/user', () => ({
  useUserStore: () => ({
    login: jest.fn(),
  }),
}));

describe('Login.vue', () => {
    let wrapper;
    let userStore;

    beforeEach(() => {
        userStore = useUserStore();
        wrapper = mount(Login);
        ElMessage.success.mockClear();
        ElMessage.error.mockClear();
        userStore.login.mockClear();
    });

    it('should render the login form correctly', () => {
        const usernameInput = wrapper.find('input[placeholder="请输入用户名"]');
        const passwordInput = wrapper.find('input[placeholder="请输入密码"]');
        const loginButton = wrapper.find('button[type="primary"]');

        expect(usernameInput.exists()).toBe(true);
        expect(passwordInput.exists()).toBe(true);
        expect(loginButton.exists()).toBe(true);
    });

    it('should show an error message when credentials are missing', async () => {
        const loginButton = wrapper.find('button[type="primary"]');
        await loginButton.trigger('click');

        expect(ElMessage.error).toHaveBeenCalledWith('用户名和密码不能为空');
    });

    it('should show an error message when invalid credentials are entered', async () => {
        const usernameInput = wrapper.find('input[placeholder="请输入用户名"]');
        const passwordInput = wrapper.find('input[placeholder="请输入密码"]');
        await usernameInput.setValue('wronguser');
        await passwordInput.setValue('wrongpass');

        const loginButton = wrapper.find('button[type="primary"]');
        await loginButton.trigger('click');

        userStore.login.mockRejectedValueOnce(new Error('登录失败'));
        await flushPromises();

        expect(userStore.login).toHaveBeenCalledWith({ username: 'wronguser', password: 'wrongpass' });
        expect(ElMessage.error).toHaveBeenCalledWith('登录失败');
    });

    it('should successfully log in when valid credentials are entered', async () => {
        const usernameInput = wrapper.find('input[placeholder="请输入用户名"]');
        const passwordInput = wrapper.find('input[placeholder="请输入密码"]');
        await usernameInput.setValue('testuser');
        await passwordInput.setValue('password123');

        const loginButton = wrapper.find('button[type="primary"]');
        await loginButton.trigger('click');

        userStore.login.mockResolvedValueOnce();
        await flushPromises();

        expect(userStore.login).toHaveBeenCalledWith({ username: 'testuser', password: 'password123' });
        expect(ElMessage.success).toHaveBeenCalledWith('登录成功');
    });

    it('should clear the password field on login failure', async () => {
        const usernameInput = wrapper.find('input[placeholder="请输入用户名"]');
        const passwordInput = wrapper.find('input[placeholder="请输入密码"]');
        await usernameInput.setValue('testuser');
        await passwordInput.setValue('password123');

        const loginButton = wrapper.find('button[type="primary"]');
        await loginButton.trigger('click');

        userStore.login.mockRejectedValueOnce(new Error('登录失败'));
        await flushPromises();

        expect(userStore.login).toHaveBeenCalledWith({ username: 'testuser', password: 'password123' });
        expect(ElMessage.error).toHaveBeenCalledWith('登录失败');
        await nextTick();
        expect(passwordInput.element.value).toBe('');
    });
});